﻿using System;
using System.Drawing;
using System.IO;
using System.Text;
using ExcelConfig.StoreAPI;

namespace configstore.cmd
{
    internal class Program
    {
        internal const string app_prefix = "cfgstore()# ";
        internal const string default_zone = "default";
        
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            Console.WriteLine("welcome, config store command!");
            ConfigStoreHelper.EnterConfigStoreAPIConfig();
            var exit = false;
            var zone = default_zone;
            var name = string.Empty;
            var field = string.Empty;
            string? cmd;
            while (!exit)
            {
                Console.Write($"cfgstore({zone}){(string.IsNullOrEmpty(name) ? string.Empty : "$"+name)}# ");
                cmd = Console.ReadLine();
                if (cmd == null) continue;
                try
                {
                    switch (cmd.ToLower())
                    {
                        case "exit": exit = true; break;
                        case "help":
                            Console.WriteLine("zone       set config zone");
                            Console.WriteLine("name       set config name");
                            Console.WriteLine("new        create new config");
                            Console.WriteLine("get        download config");
                            Console.WriteLine("set        upload config");
                            Console.WriteLine("read       read config item");
                            Console.WriteLine("write      write config item");
                            Console.WriteLine("ls         list config name");
                            Console.WriteLine("zones      list zone name");
                            break;
                        case "ls":
                            if (zone == null) zone = "default";
                            zone = zone.Trim();
                            foreach (var cfgname in ConfigStoreAPI.ListConfigName(zone))
                            {
                                Console.WriteLine(cfgname);
                            }
                            break;
                        case "zones":
                            foreach (var cfgname in ConfigStoreAPI.ListZoneName())
                            {
                                Console.WriteLine(cfgname);
                            }
                            break;
                        case "zone":
                        case "cd":
                            Console.Write("cfgstore#zone$ input zone: ");
                            zone = Console.ReadLine();
                            if (zone == null) zone = "default";
                            zone = zone.Trim();
                            break;
                        case "name":
                            Console.Write("cfgstore#new$ input name: ");
                            name = Console.ReadLine();
                            if (name == null) break;
                            name = name.Trim();
                            break;
                        case "new":
                        case "create":
                            Console.Write("cfgstore#new$ input name: ");
                            name = Console.ReadLine();
                            if (name == null) break;
                            name = name.Trim();
                            ConfigStoreAPI.NewConfig(zone, name);
                            break;
                        case "get":
                        case "download":
                            if (string.IsNullOrEmpty(name))
                            {
                                Console.Write("cfgstore#get$ input name: ");
                                name = Console.ReadLine();
                                if (name == null) break;
                                name = name.Trim();
                            }
                            if (!Directory.Exists($"./{zone}")) Directory.CreateDirectory($"./{zone}");
                            ConfigStoreAPI.DownloadToFile(zone, name, $"./{zone}/{name}.config.xlsx");
                            break;
                        case "set":
                        case "upload":
                            Console.Write("cfgstore#set$ input path: ");
                            var path = Console.ReadLine();
                            if (string.IsNullOrEmpty(path)) break;
                            if (string.IsNullOrEmpty(name))
                            {
                                Console.Write("cfgstore#set$ input name: ");
                                name = Console.ReadLine();
                                if (name == null) break;
                                name = name.Trim();
                            }
                            ConfigStoreAPI.Upload(zone, name, path, true);
                            break;
                        case "read":
                            if (string.IsNullOrEmpty(name))
                            {
                                Console.Write("cfgstore#read$ input name: ");
                                name = Console.ReadLine();
                                if (name == null) break;
                                name = name.Trim();
                            }

                            Console.Write("cfgstore#read$ input field name: ");
                            field = Console.ReadLine();
                            if (field == null) break;
                            field = field.Trim();

                            var value = ConfigStoreAPI.Read(zone, name, field);
                            Console.WriteLine($"GET-> {field} value: {value}");
                            break;
                        case "write":
                            if (string.IsNullOrEmpty(name))
                            {
                                Console.Write("cfgstore#read$ input name: ");
                                name = Console.ReadLine();
                                if (name == null) break;
                                name = name.Trim();
                            }

                            Console.Write("cfgstore#read$ input field name: ");
                            field = Console.ReadLine();
                            if (field == null) break;
                            field = field.Trim();
                            Console.Write("cfgstore#read$ input value: ");
                            var w_value = Console.ReadLine();
                            if (w_value == null) break;

                            var wok = ConfigStoreAPI.Write(zone, name, field, w_value);
                            Console.WriteLine($"GET-> {field} write: {wok}");
                            break;
                    }
                }
                catch (Exception ex)
                {
                    var c = Console.ForegroundColor;
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.WriteLine($"ERR: {ex.Message}");
                    Console.ForegroundColor = c;
                }
            }
        }
    }
}
